﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Threading;

namespace chapter4_thread_5
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("主程序中！");
            Console.WriteLine("线程的 Id: {0}", Thread.CurrentThread.ManagedThreadId);
            Console.WriteLine();

            string path = "../../demo.log";
            byte[] buffer = new byte[4096];

            System.IO.FileStream fs = new System.IO.FileStream(
                path,
                System.IO.FileMode.Open,
                System.Security.AccessControl.FileSystemRights.Read,
                System.IO.FileShare.Read,
                8,
                System.IO.FileOptions.Asynchronous);

            IAsyncResult result = fs.BeginRead(buffer, 0, 4096,
                delegate(IAsyncResult ar)
                {
                    Console.WriteLine("第三步操作中。");
                    Console.WriteLine("线程的 Id: {0}",
                        Thread.CurrentThread.ManagedThreadId);
                    int length = fs.EndRead(ar);
                    string message
                        = System.Text.Encoding.UTF8.GetString(buffer, 0, length);
                    Console.WriteLine(message);
                    Console.WriteLine();
                },
                        null);

            Console.WriteLine("第一步操作已经完成 。现在主程序继续进行！");
            Console.WriteLine("线程的 Id: {0}", Thread.CurrentThread.ManagedThreadId);

            Console.WriteLine("注意，线程池线程是背景线程，所以主线程不能结束，回车后结束程序。");
            Console.WriteLine();

            Console.ReadLine();
        }

        private static void APM_4_3()
        {
            Console.WriteLine("主程序中！");
            Console.WriteLine("线程的 Id: {0}", Thread.CurrentThread.ManagedThreadId);
            Console.WriteLine();

            string path = "../../demo.log";
            byte[] buffer = new byte[4096];

            System.IO.FileStream fs = new System.IO.FileStream(
                path,
                System.IO.FileMode.Open,
                System.Security.AccessControl.FileSystemRights.Read,
                System.IO.FileShare.Read,
                8,
                System.IO.FileOptions.Asynchronous);
            IAsyncResult result = fs.BeginRead(buffer, 0, 4096,
                null,
                null);

            Console.WriteLine("第一步操作已经完成。现在主程序继续进行！");
            Console.WriteLine("线程的 Id: {0}", Thread.CurrentThread.ManagedThreadId);


            int length = fs.EndRead(result);
            string message = System.Text.Encoding.UTF8.GetString(buffer, 0, length);
            Console.WriteLine(message);
            Console.WriteLine();

            Console.WriteLine("注意，此时第三步操作已经完成，回车后结束程序。");
            Console.WriteLine();

            Console.ReadLine();
        }

        private static void APM_4_2()
        {
            Console.WriteLine("主程序中！");
            Console.WriteLine("线程的 Id: {0}", Thread.CurrentThread.ManagedThreadId);
            Console.WriteLine();

            string path = "../../demo.log";
            byte[] buffer = new byte[4096];

            System.IO.FileStream fs = new System.IO.FileStream(
                path,
                System.IO.FileMode.Open,
                System.Security.AccessControl.FileSystemRights.Read,
                System.IO.FileShare.Read,
                8,
                System.IO.FileOptions.Asynchronous);
            IAsyncResult result = fs.BeginRead(buffer, 0, 4096,
                null,
                null);

            Console.WriteLine("第一步操作已经完成。现在主程序继续进行！");
            Console.WriteLine("线程的 Id: {0}", Thread.CurrentThread.ManagedThreadId);

            result.AsyncWaitHandle.WaitOne();
            int length = fs.EndRead(result);
            string message = System.Text.Encoding.UTF8.GetString(buffer, 0, length);
            Console.WriteLine(message);
            Console.WriteLine();

            Console.WriteLine("注意，此时第三步操作已经完成，回车后结束程序。");
            Console.WriteLine();

            Console.ReadLine();
        }

        private static void APM_4_1()
        {
            Console.WriteLine("主程序中！");
            Console.WriteLine("线程的 Id: {0}", Thread.CurrentThread.ManagedThreadId);
            Console.WriteLine();

            string path = "../../demo.log";
            byte[] buffer = new byte[4096];

            System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open);
            IAsyncResult result = fs.BeginRead(buffer, 0, 4096,
                delegate(IAsyncResult ar)
                {
                    Console.WriteLine("第三步操作中。");
                    Console.WriteLine("线程的 Id: {0}", Thread.CurrentThread.ManagedThreadId);
                    int length = fs.EndRead(ar);
                    string message = System.Text.Encoding.UTF8.GetString(buffer, 0, length);
                    Console.WriteLine(message);
                    Console.WriteLine();
                },
                null);

            Console.WriteLine("第一步操作已经完成。现在主程序继续进行！");
            Console.WriteLine("线程的 Id: {0}", Thread.CurrentThread.ManagedThreadId);
            Console.WriteLine();

            // 等待第二个线程完成
            while (!result.IsCompleted)
            {
                Thread.Sleep(10);
            }

            Console.WriteLine("注意，此时第三步操作已经完成，回车后结束程序。");
            Console.WriteLine();

            Console.ReadLine();
        }


    }
}
